﻿$(function() {
	$.datepicker.setDefaults($.extend({ showMonthAfterYear: false }, $.datepicker.regional['zh-CN']));
	$("#date").datepicker({ changeMonth: true, changeYear: true/*,showOn: 'button', buttonImage: '/support/Alpha/images/calendar-icon_calendar.png', buttonImageOnly: true*/});
});
function OnEditHeight()
{
    ShowRangeDisplay(["ID_INPUT_HEIGHT","ID_CANCEL_HEIGHT","ID_ADD_HEIGHT"],true);
    ShowRangeDisplay(["ID_HEIGHT","ID_MODITY_HEIGHT"],false);
    _$("ID_INPUT_HEIGHT").value= _$("ID_HEIGHT").innerHTML ;
}
function OnCancelHeight()
{
    ShowRangeDisplay(["ID_INPUT_HEIGHT","ID_CANCEL_HEIGHT","ID_ADD_HEIGHT"],false);
    ShowRangeDisplay(["ID_HEIGHT","ID_MODITY_HEIGHT"],true);
}
function OnSaveHeight()
{
    function OnSuc(xmlDoc) {
        $.prompt('修改成功！', { buttons: { 关闭: true }, focus: 1 });
        ShowRangeDisplay(["ID_INPUT_HEIGHT","ID_CANCEL_HEIGHT","ID_ADD_HEIGHT"],false);
        ShowRangeDisplay(["ID_HEIGHT", "ID_MODITY_HEIGHT"], true);
        _$("ID_HEIGHT").innerHTML=iHeight;
    }
    function OnFail(xmlDoc)
    {
        var sMsg = xmlDoc.getElementsByTagName("result")[0].getAttribute("msg");
        $.prompt(sMsg, { buttons: { 关闭: true }, focus: 1 });
    }
    function OnError()
    {
        $.prompt('操作失败，请稍后再试', { buttons: { 关闭: true }, focus: 1 });
    }
    var iHeight=parseInt(_$("ID_INPUT_HEIGHT").value) ;
    if (iHeight < 0 &&　iHeight >250) {
        $.prompt('身高填写有误，请检查', { buttons: { 关闭: true }, focus: 1 });
        return;
    }
    var sUrl="HeightUpdate.aspx?height="+iHeight;
    getResultXml(sUrl, OnSuc, OnFail, OnError);
}
function OnSaveWeight()
{
    function OnSuc(xmlDoc)
    {
        $.prompt('添加成功', { buttons: { 关闭: true }, 
                               focus: 1,
                               callback: function(v, m, f){
                                if (v) {
                                    window.location.href = window.location.href;
                                }
                             }});
    }
    function OnFail(xmlDoc)
    {
        var sMsg = xmlDoc.getElementsByTagName("result")[0].getAttribute("msg");
        var code = xmlDoc.getElementsByTagName("result")[0].getAttribute("code");
        if (-103 == code) {
            $.prompt(sMsg + "您现在去修改发病时间吗?", { buttons: { 去: true, 不去: false }, 
                                                          focus: 1,
                                                          callback: function(v, m, f){
                                                           if (v) {
                                                               window.location.href = "../DiseaseHistory/Index.aspx";
                                                           }
                                                        }});
        }
        else {
            $.prompt(sMsg, { buttons: { 关闭: true }, focus: 1 });
        }
    }
    function OnError()
    {
        $.prompt("操作失败，请稍后再试", { buttons: { 关闭: true }, focus: 1 });
    }
    var date = _$("date").value ;
    var sUrl="WeightUpdate.aspx?optype=1&date="+date.escUrl()+"&value="+_$("ID_INPUT_WEIGHT").value;
    getResultXml(sUrl, OnSuc, OnFail, OnError);
}
function OnDelWeightHistory(id)
{
    function OnSuc(xmlDoc) {
        $.prompt('删除成功', { buttons: { 关闭: true },
            focus: 1,
            callback: function(v, m, f) {
                if (v) {
                    window.location.href = window.location.href;
                }
            } 
        });
    }
    function OnFail(xmlDoc)
    {
        var sMsg = xmlDoc.getElementsByTagName("result")[0].getAttribute("msg");
        var code =  xmlDoc.getElementsByTagName("result")[0].getAttribute("code");
        $.prompt(sMsg, { buttons: { 关闭: true }, focus: 1 });
    }
    function OnError()
    {
        $.prompt("操作失败，请稍后再试", { buttons: { 关闭: true }, focus: 1 });      
    }
    $.prompt("你确定要删除此条记录吗？", { buttons: { 确定: true, 取消: false }, focus: 1, callback: function(v, m, f) {
        if (v) {
            var date = _$("date").value;
            var sUrl = "WeightUpdate.aspx?optype=3&weighthistoryid=" + id;
            getResultXml(sUrl, OnSuc, OnFail, OnError);
        }
    } 
    });
}
function OnEditWeight(id, datetime, weight) {
    var formstr = '<p>修改' + datetime + '的体重:</p>' +
					'<div class="field"><input type="text" name="modifyweight" id="modifyweight" value="' + weight + '" /></div>公斤';
    var states = {
        state0: {
            html: formstr,
            buttons: { 确定: true, 取消: false },
            focus: 1,
            submit: function(v, m, f) {
                if (v) {
                    function OnSuc(xmlDoc) {
                        jQuery.prompt.goToState('state1');
                    }
                    function OnFail(xmlDoc) {
                        var msg = xmlDoc.getElementsByTagName("result")[0].getAttribute("msg");
                        states["state2"]["html"] = msg;
                        jQuery.prompt.goToState('state2');

                    }
                    function OnError() {
                        states["state2"]["html"] = "操作失败，请稍后再试！";
                        $.prompt.goToState('state2');
                    }
                    var value = m.find('#modifyweight').val();
                    var sUrl = "WeightUpdate.aspx?optype=2&weighthistoryid=" + id + "&date=" + datetime + "&value=" + value;
                    getResultXml(sUrl, OnSuc, OnFail, OnError);
                    return false;
                }
                else $.prompt.close();
            }
        },
        state1: {
            html: '修改成功',
            buttons: { 关闭: true },
            focus: 1,
            submit: function(v, m, f) { $.prompt.close(); window.location.href = window.location.href; }
        },
        state2: {
            html: '修改失败',
            buttons: { 关闭: true },
            focus: 1,
            submit: function(v, m, f) { $.prompt.close(); }
        }
    }
$.prompt(states);
}